package net.begincode.servlet.chapter;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import utils.BQStringUtil;
import utils.CacheUtils;
import utils.JsoupUtil;
import utils.ViewCount;

import com.google.gson.Gson;

import net.begincode.bean.BcCourse;
import net.begincode.bean.BcUser;
import net.begincode.bean.CourseChapter;
import net.begincode.servlet.common.BaseServlet;
import net.begincode.servlet.common.ViewServlet;

public class ChapterServlet extends BaseServlet{
	private static final Log LOG = LogFactory.getLog(ChapterServlet.class);
	
	/**
	 * 新增章节初始化
	 * */
	public void addChapterInit(HttpServletRequest request, HttpServletResponse response){
		request.setAttribute("courses", this.courseService.selAllCourse());
		try {
			request.getRequestDispatcher("/WEB-INF/manage/editChapter.jsp").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 新增章节保存
	 * @throws UnsupportedEncodingException 
	 * */
	public void saveChapter(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException{
		CourseChapter chapter  = new CourseChapter();
		chapter.setBc_course_id(Integer.valueOf(request.getParameter("courseId")));
		chapter.setChapter_title(JsoupUtil.jsoupFilter(request.getParameter("ch_title")));
		chapter.setCha_abstract(JsoupUtil.jsoupFilter(request.getParameter("ch_abstract")));
		chapter.setChapter_content(request.getParameter("ch_content"));
		chapter.setChapter_keyword(JsoupUtil.jsoupFilter(request.getParameter("ch_keyword")));
		Map<String,String> userMap = this.getCookieInfo(request, response);
		//初始化部分数据
		chapter.setPraise(0);
		chapter.setView_count(0);
		chapter.setWriter(userMap.get("nickName"));
		chapter.setChapter_weight(0);
		chapter.setDiscuss_flag("1");
		chapter.setBc_user_id(Integer.valueOf(userMap.get("bcUserId").toString()));;
		chapter.setDiscuss_flag("1");
		chapter.setChapter_resouce("");
		String chapterId = request.getParameter("chapterId");
		int returnValue = 0;
		if(chapterId.equals("")){
			returnValue = this.chapterService.addChapter(chapter);
		}else{
			chapter.setCourse_chapter_id(Integer.parseInt(chapterId));;
			returnValue = this.chapterService.updChaper(chapter);
		}
		 
		if(returnValue > 0){
			
			request.setAttribute("msg", "保存成功");
		}else{
			request.setAttribute("chapter", chapter);
			request.setAttribute("msg", "保存失败");
		}
		try {
			request.setAttribute("courses", this.courseService.selAllCourse());
			request.getRequestDispatcher("/WEB-INF/manage/editChapter.jsp").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	/**
	 * 查看章节
	 * @throws IOException 
	 * @throws ServletException 
	 * */
	public void viewChapter(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		String chapterId = (String)request.getAttribute("id");
		ViewCount.addViewCount(ViewServlet.VIEW_CHAPTER+":"+chapterId); //添加阅读数
		CourseChapter chapter =  this.chapterService.selChapterCache(chapterId);
		request.setAttribute("chapter", chapter);
		request.setAttribute("relchapter", this.chapterService.selSameChapterLimitFiveCache(chapter.getBc_course_id(), chapter.getChapter_weight()));
		request.setAttribute("relCodes", this.codeService.selNewCodeByChapterIdCache(chapterId, 5));
		request.getRequestDispatcher("/WEB-INF/page/chapter_view.jsp").forward(request, response);
	}
	/**
	 * 查询所有章节
	 * @throws IOException 
	 * @throws ServletException 
	 * */
	public void selAllChapter(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		request.setAttribute("chapters", this.chapterService.selChapterByParam(null));
		request.getRequestDispatcher("/WEB-INF/manage/chapterManage.jsp").forward(request, response);
	}
	/**
	 * 修改章节初始化
	 * @throws IOException 
	 * @throws ServletException 
	 * */
	public void updChapterInit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		request.setAttribute("courses", this.courseService.selAllCourse());
		String chapterId = request.getParameter("chapterId");
		CourseChapter cc = this.chapterService.selChapter(chapterId);
		cc.setChapter_content(BQStringUtil.htmlspecialchars(cc.getChapter_content()));
		request.setAttribute("chapter", cc);
		request.getRequestDispatcher("/WEB-INF/manage/editChapter.jsp").forward(request, response);
	}

}
